package com.lj.infoisland.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lj.infoisland.common.convention.result.Result;
import com.lj.infoisland.common.convention.result.Results;
import com.lj.infoisland.domain.dto.CommentDTO;
import com.lj.infoisland.domain.po.NewsComments;
import com.lj.infoisland.domain.po.Users;
import com.lj.infoisland.domain.vo.UserInfoVO;
import com.lj.infoisland.service.NewsCommentsService;
import com.lj.infoisland.service.NewsService;
import com.lj.infoisland.service.UsersService;
import com.lj.infoisland.util.UserContext;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.lj.infoisland.domain.dto.CommentDTO.buildCommentTree;

/*
 * @description 评论控制层
 * @author 龙洁
 * @create 2024-10-27 上午11:14
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Value("${minio.FILE_PREFIX}")
    private String FILE_PREFIX;
    @Resource
    private NewsService newsService;
    @Resource
    private NewsCommentsService newsCommentsService;
    @Resource
    private UsersService usersService;

    /**
     * 获取评论列表
     *
     * @param newsId 新闻id
     * @return 评论列表
     */
    @GetMapping("/getCommentList")
    public Result<?> getCommentList(@RequestParam("newsId") String newsId) {
        LambdaQueryWrapper<NewsComments> eq = Wrappers.lambdaQuery(NewsComments.class)
                .eq(NewsComments::getNewsId, newsId);
        List<NewsComments> list = newsCommentsService.list(eq);
        List<CommentDTO> commentDTOS = new ArrayList<>();
        list.forEach(item -> {
            CommentDTO bean = BeanUtil.toBean(item, CommentDTO.class);
            UserInfoVO data = usersService.getUserInfo(item.getUserUniqueId()).getData();
            bean.setUsername(data.getUsername());
            bean.setAvatarUrl(data.getAvatarUrl());
            commentDTOS.add(bean);
        });
        // 组装评论
        return Results.success(buildCommentTree(commentDTOS));
    }

    /**
     * 获取评论列表page
     *
     * @return 评论列表
     */
    @GetMapping("/getCommentPageList")
    public Result<?> getCommentPageList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<NewsComments> eq = Wrappers.lambdaQuery(NewsComments.class)
                .orderByDesc(NewsComments::getCreatedAt);
        Page<NewsComments> page = new Page<>(pageNum, pageSize);
        List<NewsComments> list = newsCommentsService.page(page, eq).getRecords();
        List<CommentDTO> commentDTOS = new ArrayList<>();
        list.forEach(item -> {
            CommentDTO bean = BeanUtil.toBean(item, CommentDTO.class);
            UserInfoVO data = usersService.getUserInfo(item.getUserUniqueId()).getData();
            bean.setUsername(data.getUsername());
            bean.setAvatarUrl(data.getAvatarUrl());
            commentDTOS.add(bean);
        });
        // 组装评论
        return Results.success(commentDTOS);
    }

    /**
     * 获取评论列表page
     *
     * @return 评论列表
     */
    @GetMapping("/getCommentTotal")
    public Result<?> getCommentTotal() {
        long count = newsCommentsService.count();
        // 组装评论
        return Results.success(count);
    }

    /**
     * 发送评论
     *
     * @param comment 评论内容
     */
    @PostMapping("/sendComment")
    public Result<Void> sendComment(@RequestBody CommentDTO comment) {
        // 文章id,评论内容,userUniqueId,parentId(可以为空)
        NewsComments newsComments = NewsComments.builder()
                .newsId(comment.getNewsId())
                .userUniqueId(UserContext.getUser().getUserUniqueId())
                .content(comment.getContent())
                .target(comment.getTarget())
                .parentId(comment.getParentId() == 0 ? null : comment.getParentId())
                .build();
        newsCommentsService.save(newsComments);
        return Results.success();
    }
}
